﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class bzjdInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        CustomGridView CustomGridView1;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack)
            {
                Bind();
            }
            CustomGridView1.OnLoadData+=new EventHandler(CustomGridView1_OnLoadData);
            Button btnSeach = (Button)FindControl("btnSeach");
            btnSeach.Click += new EventHandler(Go_Search);
            Button btnRY = (Button)FindControl("btnRY");
            btnRY.Click += new EventHandler(btnRY_Click);
            foreach (GridViewRow gvr in CustomGridView1.Rows) {
                Button btn_tj = (Button)gvr.FindControl("btn_tj");
                Button btn_shtg = (Button)gvr.FindControl("btn_shtg");
                Button btn_tj2 = (Button)gvr.FindControl("btn_tj2");
                btn_tj.Click += new EventHandler(ChangeStatus);
                btn_shtg.Click += new EventHandler(ChangeStatus1);
                btn_tj2.Click += new EventHandler(ChangeStatus2);
            }

        }
        protected void InitControl() {
            CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
        }
        
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        
        protected void Bind()
        {
            string pID = base.Page.Request["PNID"];
            CustomGridView1.RowDataBound += new GridViewRowEventHandler(CustomGridView1_RowDataBound);
            CustomGridView1.RowCreated += new GridViewRowEventHandler(CustomGridView1_RowCreated);
            DataTable dt = _EluZSDataProvider.GetBCJDRYXX(pID,GetStrWhere());
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.PageSize = 10;
            CustomGridView1.DataKeyNames = new string[] { "ID", "提交状态", "审批状态" };
            CustomGridView1.DataBind();
            
        }

        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow gvr in CustomGridView1.Rows)
            {
                if (CustomGridView1.DataKeys[gvr.RowIndex].Values["提交状态"].ToString() == "0")
                {
                    Button btn = (Button)gvr.FindControl("btn_tj");
                    btn.Enabled = true;
                }
                else
                {
                    Button btn2 = (Button)gvr.FindControl("btn_tj2");
                    btn2.Enabled = true;
                }
                if (CustomGridView1.DataKeys[gvr.RowIndex].Values["审批状态"].ToString() == "1")
                {
                    Button btn2 = (Button)gvr.FindControl("btn_tj2");
                    btn2.Enabled = false;
                }
            }
        }
        protected void CustomGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string strTJ = rowview["提交状态"].ToString();
                string strSHTG = rowview["审批状态"].ToString();
                Button btn1 = (Button)e.Row.FindControl("btn_shtg");
                if (strTJ == "1") { btn1.Enabled = true; }
                if (strSHTG == "1") { btn1.Enabled = false; }
            }
        }

        protected void btnRY_Click(object sender, EventArgs e)
        {
            string pid = base.Page.Request["PNID"].ToString();
            string values = "JDSelect.aspx?PID=" + pid + "";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "selecthouse", string.Format("<script>SelectHouse('{0}');</script>", values));
            Bind();
        }
        protected string GetStrWhere() {
            string strwhere = string.Empty;
            TextBox txtPerName = (TextBox)FindControl("产权人");
            TextBox txtFWZL = (TextBox)FindControl("房屋坐落");
            HiddenField hfdQqh = (HiddenField)FindControl("hfdQqh");
            string perName = txtPerName.Text.Trim();
            string perfwzl = txtFWZL.Text.Trim();
            if (!string.IsNullOrEmpty(perName))
            {
                strwhere += " and 产权人 like '%" + perName + "%'";
            }
            if (!string.IsNullOrEmpty(perfwzl))
            {
                strwhere += " and 坐落 like '%" + perfwzl + "%'";
            }
            if (!string.IsNullOrEmpty(hfdQqh.Value))
            {
                strwhere += string.Format(" and t.personid in ('{0}')", hfdQqh.Value.Replace("|", "','"));
            }
            return strwhere;
        }
        protected void ChangeStatus(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string strID = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            string temp = _EluZSDataProvider.GetSomeStr("ZS_BCJD", "提交状态", "id", strID);
            string temp1 = _EluZSDataProvider.GetSomeStr("ZS_BCJD", "审批状态", "id", strID);
            if (temp == "0")
            {
                bool change = _EluZSDataProvider.UpdateBSZT("ZS_BCJD", "提交状态", "1", "id", strID);
                if (change)
                {
                    Bind();
                }
            }
            else
            {
                Bind();
            }
        }
        protected void ChangeStatus1(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string strID = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.UpdateBSZT("ZS_BCJD", "审批状态", "1", "id", strID);
            if (change)
            {
                Bind();
            }
        }
        protected void ChangeStatus2(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string strID = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.UpdateBSZT("ZS_BCJD", "提交状态", "0", "id", strID);
            if (change) { Bind(); }
        }
    }
}


